#Make kernel img

#entry point of kernel
KERNELENTRY	=	0x30400

#offset of entry point int kernel file
ENTRYOFFSET	=	0x400

#run and debug file
BOCHSFILE = OS.bxrc
#Compilers,flags
ASM	= nasm
CC	= gcc
LD	= ld
LDFLAGS = -s -Ttext $(KERNELENTRY)
ASMBFLAGS	= -I defines/
ASMKFLAGS	= -f elf
CFLAGS		= -I include/ -c -fno-builtin -fno-stack-protector
#OBJS
OBJS = boot/boot.o boot/loader.o
KOBJS = kernel/kernel.o kernel/global.o kernel/exception.o kernel/initEI.o kernel/interrupt.o kernel/start.o kernel/main.o kernel/process.o kernel/protect.o kernel_lib/string.o kernel_lib/klibc.o kernel_lib/klib.o kernel/syscall.o kernel/keyboard.o kernel/tty.o
KBIN = kernel/kernel.bin

.PHONY:all clean img allclean final run copy

all : $(OBJS) $(KOBJS) $(KBIN)

copy:
	rm -f /home/jack/os/runos/boot.img
	cp boot.img /home/jack/os/runos/boot.img
clean:
	rm -f $(OBJS) $(KOBJS) $(KBIN)
allclean:
	rm -f $(OBJS) $(KOBJS) $(KBIN) boot.img
img:
	cat boot/boot.o boot/loader.o kernel/kernel.bin extra/ex.bin>boot.img
	dd if=/dev/zero of=./floppy.img bs=1440k count=1
	dd if=./boot.img of=./floppy.img bs=1440k count=1 conv=notrunc
final: all img clean

run : 
	bochs -qf $(BOCHSFILE)

boot/boot.o : boot/boot.asm defines/bootconst.inc
	$(ASM) $(ASMBFLAGS) -o $@ $<
boot/loader.o : boot/loader.asm defines/pmc.inc
	$(ASM) $(ASMBFLAGS) -o $@ $<

kernel/kernel.bin : $(KOBJS)
	$(LD) $(LDFLAGS) -o $@ $(KOBJS)

kernel/kernel.o : kernel/kernel.asm
	$(ASM) $(ASMKFLAGS) -o $@ $<
kernel/syscall.o : kernel/syscall.asm
	$(ASM) $(ASMKFLAGS) -o $@ $<
kernel/global.o : kernel/global.c include/type.h include/const.h include/protectmode.h include/global.h include/keyboard.h
	$(CC) $(CFLAGS) -o $@ $<
kernel/exception.o: kernel/exception.c include/type.h include/const.h include/protectmode.h include/global.h include/prototype.h
	$(CC) $(CFLAGS) -o $@ $<
kernel/initEI.o : kernel/initEI.c include/type.h include/const.h include/protectmode.h include/global.h include/prototype.h
	$(CC) $(CFLAGS) -o $@ $<
kernel/interrupt.o : kernel/interrupt.c include/type.h include/const.h include/protectmode.h include/process.h include/global.h include/prototype.h
	$(CC) $(CFLAGS) -o $@ $<
kernel/start.o : kernel/start.c include/type.h include/const.h include/protectmode.h include/global.h include/prototype.h include/string.h
	$(CC) $(CFLAGS) -o $@ $<
kernel/main.o : kernel/main.c include/type.h include/const.h include/protectmode.h include/global.h include/prototype.h include/string.h include/process.h
	$(CC) $(CFLAGS) -o $@ $<
kernel/process.o : kernel/process.c include/type.h include/const.h include/protectmode.h include/prototype.h include/string.h include/process.h
	$(CC) $(CFLAGS) -o $@ $<
kernel/keyboard.o : kernel/keyboard.c include/type.h include/const.h include/protectmode.h include/prototype.h include/keyboard.h include/keymap.h include/tty.h include/global.h
	$(CC) $(CFLAGS) -o $@ $<
kernel/tty.o : kernel/tty.c include/type.h include/const.h include/protectmode.h include/prototype.h include/keyboard.h include/tty.h include/global.h
	$(CC) $(CFLAGS) -o $@ $<
kernel/protect.o : kernel/protect.c include/type.h include/const.h include/protectmode.h include/global.h include/prototype.h include/string.h
	$(CC) $(CFLAGS) -o $@ $<
kernel_lib/string.o : kernel_lib/string.asm
	$(ASM) $(ASMKFLAGS) -o $@ $<
kernel_lib/klibc.o : kernel_lib/klib.c include/type.h include/const.h include/global.h
	$(CC) $(CFLAGS) -o $@ $<
kernel_lib/klib.o : kernel_lib/klib.asm
	$(ASM) $(ASMKFLAGS) -o $@ $<


